import React, { Component } from "react";
import { Link, Route, Switch } from "react-router-dom";

class Home1 extends Component {
  render() {
    return <div>这是Home1组件</div>;
  }
}
class Home2 extends Component {
  render() {
    return <div>这是Home2组件</div>;
  }
}

class Home extends Component {
  render() {
    // this.props打印的就是路由信息
    // console.log(this.props);
    const {
      match: { url },
    } = this.props;
    return (
      <>
        <h4>这是home组件</h4>
        <ul>
          <li>
            <Link to={`${url}/home1`}>home1</Link>
          </li>
          <li>
            <Link to={`${url}/home2`}>home2</Link>
          </li>
        </ul>

        <hr />

        <Switch>
          <Route path={`${url}/home1`} component={Home1}></Route>
          <Route path={`${url}/home2`} component={Home2}></Route>
        </Switch>
      </>
    );
  }
}

class About extends Component {
  render() {
    return <div>这是About组件</div>;
  }
}

class Mine extends Component {
  render() {
    return <div>这是Mine组件</div>;
  }
}

class App extends Component {
  render() {
    return (
      <>
        <h2>react-router-嵌套路由</h2>
        <ul>
          <li>
            <Link to="/home">首页</Link>
          </li>
          <li>
            <Link to="/about">关于页</Link>
          </li>
          <li>
            <Link to="/mine">我的页</Link>
          </li>
        </ul>

        <hr />

        <Switch>
          {/* 如果一个路由是要做嵌套的，那么它不能加exact变成精准匹配 */}
          <Route path="/home" component={Home}></Route>
          <Route path="/about" component={About}></Route>
          <Route path="/mine" component={Mine}></Route>
        </Switch>
      </>
    );
  }
}

export default App;
